package com.wx.forum.service.impl;

import com.wx.forum.common.AppResult;
import com.wx.forum.common.ResultCode;
import com.wx.forum.dao.MessageMapper;
import com.wx.forum.exception.ApplicationException;
import com.wx.forum.model.Message;
import com.wx.forum.model.User;
import com.wx.forum.service.IMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class MessageServicelmpl implements IMessageService {
    @Resource
    private MessageMapper messageMapper;
    @Resource
    private UserServiceImpl userService;
    /**
     * 发送一条私信
     * @param message
     */
    @Override
    public void create(Message message) {
        //1.参数校验
        if(message == null || message.getPostUserId() == null ||
           message.getReceiveUserId() == null || message.getContent() == null){
            //打印信息
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.校验接收者是否存在
        User receiveUser = userService.selectById(message.getReceiveUserId());
        if(receiveUser == null || receiveUser.getDeleteState() == 1){
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }

        //3.设置默认值
        message.setState((byte) 0);
        message.setDeleteState((byte) 0);
        Date date = new Date();
        message.setCreateTime(date);
        message.setUpdateTime(date);

        //4.调用dao
        int row = messageMapper.insertSelective(message);
        if(row != 1){
            //打印信息
            log.warn(ResultCode.FAILED_CREATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
    }

    @Override
    public Integer selectUnreadCount(Long receiveUserId) {
        //1.参数校验
        if(receiveUserId == null || receiveUserId <= 0){
            //打印信息
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.校验接收者是否存在
        User receiveUser = userService.selectById(receiveUserId);
        if(receiveUser == null || receiveUser.getDeleteState() == 1){
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }

        //3.调用dao
        Integer count = messageMapper.selectUnreadCount(receiveUserId);
        if(count == null){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        return count;
    }

    @Override
    public List<Message> selectByReceiveId(Long receiveUserId) {
        //1.参数校验
        if(receiveUserId == null || receiveUserId <= 0){
            //打印信息
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.校验接收者是否存在
        User receiveUser = userService.selectById(receiveUserId);
        if(receiveUser == null || receiveUser.getDeleteState() == 1){
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }

        //3.调用dao,查询站内信列表
        List<Message> messageList = messageMapper.selectByReceiveId(receiveUserId);
        if(messageList == null){
            messageList = new ArrayList<>();
        }
        return messageList;
    }

    /**
     * 根据站内信id更新站内信状态
     * @param id
     * @param state
     */
    public void updateStateById(Long id,Byte state){
        //1.参数校验
        //0-未读,1-已读,2-已回复(这里可以定义成枚举)
        if(id == null || id <= 0 || state < 0 || state > 2){
            //打印信息
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.构造更新对象
        Message updateMessage = new Message();
        updateMessage.setId(id);
        updateMessage.setState(state);
        Date date = new Date();
        updateMessage.setUpdateTime(date);

        //3.调用dao
        int row = messageMapper.updateByPrimaryKeySelective(updateMessage);
        if(row != 1){
            //打印信息
            log.warn(ResultCode.FAILED_CREATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
    }

    /**
     * 根据id查询站内信
     * @param id
     * @return
     */
    public Message selectById(Long id){
        if(id == null || id <= 0){
            //打印信息
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return messageMapper.selectByPrimaryKey(id);
    }
    /**
     * 回复站内信
     * @param repiledId 要回复的对象
     * @param message 回复的站内信内容
     */
    @Override

    public void reply(Long repiledId, Message message) {
        //1.参数校验
        if(repiledId == null || repiledId <= 0){
            //打印信息
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //2.校验站内信是否存在
        Message existsMessage = messageMapper.selectByPrimaryKey(repiledId);
        if(existsMessage == null || existsMessage.getDeleteState() == 1){
            //打印信息
            log.warn(ResultCode.FAILED_MESSAGE_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS));
        }
        //3.更新回复的站内信状态为已读
        updateStateById(repiledId, (byte) 2);
        //4.添加新的站内信
        create(message);
    }


}
